package com.cskaoyan.service.promoteservice;

import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.promotebean.Groupon_rules;
import com.cskaoyan.bean.promotebean.Groupon_rulesExample;
import com.cskaoyan.bo.promoteBO.GrouponrulesBO;
import com.cskaoyan.mapper.goodsmapper.GoodsMapper;
import com.cskaoyan.mapper.promotemapper.Groupon_rulesMapper;
import com.cskaoyan.vo.promotevo.Groupon_rulesVO;
import com.cskaoyan.vo.promotevo.GrouponrulesCreateVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: feisl
 * @Date: 2021/5/10 1:17
 */
@Service
public class GrouponServiceImpl implements GrouponService{

    @Autowired
    Groupon_rulesMapper groupon_rulesMapper;
    @Override
    public Groupon_rulesVO queryGrouponRulesAll(Integer page, Integer limit, Integer goodsId,String sort, String order) {
        PageHelper.startPage(page, limit);
        Groupon_rulesExample groupon_rulesExample = new Groupon_rulesExample();
        Groupon_rulesExample.Criteria criteria = groupon_rulesExample.createCriteria();
        if(goodsId != null)criteria.andGoodsIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);

        groupon_rulesExample.setOrderByClause(sort + " " + order);
        List<Groupon_rules> groupon_rulesList = groupon_rulesMapper.selectByExample(groupon_rulesExample);

        PageInfo<Groupon_rules> groupon_rulesPageInfo= new PageInfo<>(groupon_rulesList);
        long total = groupon_rulesPageInfo.getSize();
        Groupon_rulesVO groupon_rulesVO = new Groupon_rulesVO();
        groupon_rulesVO.setItems(groupon_rulesList);
        groupon_rulesVO.setTotal(total);
        return groupon_rulesVO;
    }



    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public GrouponrulesCreateVO insert(GrouponrulesBO groupon_rulesBO) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = simpleDateFormat.parse(groupon_rulesBO.getExpireTime());
        Groupon_rules groupon_rules = new Groupon_rules();
        groupon_rules.setDiscount(groupon_rulesBO.getDiscount());
        groupon_rules.setDiscountMember( groupon_rulesBO.getDiscountMember());
        groupon_rules.setExpireTime(time);
        groupon_rules.setGoodsId(groupon_rulesBO.getGoodsId());
        groupon_rules.setAddTime(new Date());
        groupon_rules.setUpdateTime(new Date());
        groupon_rules.setDeleted(false);
        int id = groupon_rules.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        groupon_rules.setGoodsName(goods.getName());
        groupon_rules.setPicUrl(goods.getPicUrl());
        groupon_rulesMapper.insertSelective(groupon_rules);


        String format = simpleDateFormat.format(groupon_rules.getAddTime());
        String format1 = simpleDateFormat.format(groupon_rules.getUpdateTime());
        String format2 = simpleDateFormat.format(groupon_rules.getExpireTime());
        GrouponrulesCreateVO grouponrulesCreateVO = new GrouponrulesCreateVO(
                groupon_rules.getId(),groupon_rules.getGoodsId(),groupon_rules.getGoodsName(),groupon_rules.getPicUrl(),
                groupon_rules.getDiscount(),groupon_rules.getDiscountMember(),format,format1,format2
        );
        return grouponrulesCreateVO;
    }

    @Override
    public void update(GrouponrulesBO grouponrulesBO) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = simpleDateFormat.parse(grouponrulesBO.getExpireTime());
        Groupon_rules groupon_rules = new Groupon_rules();
        groupon_rules.setGoodsId(grouponrulesBO.getGoodsId());
        groupon_rules.setDiscount(grouponrulesBO.getDiscount());
        groupon_rules.setDiscountMember(grouponrulesBO.getDiscountMember());
        groupon_rules.setExpireTime(time);
        groupon_rules.setUpdateTime(grouponrulesBO.getUpdateTime());
        groupon_rules.setAddTime(grouponrulesBO.getAddTime());
        groupon_rules.setDeleted(grouponrulesBO.getDeleted());
        groupon_rules.setGoodsName(grouponrulesBO.getGoodsName());
        groupon_rules.setId(grouponrulesBO.getId());
        groupon_rules.setPicUrl(grouponrulesBO.getPicUrl());
        groupon_rulesMapper.updateByPrimaryKeySelective(groupon_rules);
    }

    @Override
    public void delete(Integer id) {
        groupon_rulesMapper.deleteByPrimaryKey(id);
    }


}
